package com.efast.dmom.equipment.service.checking.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.equipment.bean.EquipCheckItemParsmBean;
import com.efast.dmom.equipment.bean.EquipCheckingProgramOrder;
import com.efast.dmom.equipment.bean.EquipCheckingProgramStandard;
import com.efast.dmom.equipment.bean.EquipCheckingTaskBean;
import com.efast.dmom.equipment.bean.EquipCheckingTaskDetailsBean;
import com.efast.dmom.equipment.bean.checkplan.EquipCheckPlanBean;
import com.efast.dmom.equipment.bean.checkplan.EquipCheckingTaskEquipBean;
import com.efast.dmom.equipment.dao.checking.EquipCheckingOrderDao;
import com.efast.dmom.equipment.dao.checking.EquipCheckingRuleDao;
import com.efast.dmom.equipment.dao.checking.EquipCheckingTaskDao;
import com.efast.dmom.equipment.entity.EquipCheckingItems;
import com.efast.dmom.equipment.entity.EquipCheckingTask;
import com.efast.dmom.equipment.entity.EquipCheckingTaskProgram;
import com.efast.dmom.equipment.entity.EquipCheckingTaskProgramItem;
import com.efast.dmom.equipment.service.checking.IEquipCheckingOrderService;
import com.efast.dmom.equipment.util.ExplanPlan;

@Service
public class EquipCheckingOrderServiceImpl implements IEquipCheckingOrderService {

	@Autowired
	private EquipCheckingRuleDao equipCheckingRuleDao;

	@Autowired
	private EquipCheckingOrderDao equipCheckingOrderDao;

	@Autowired
	private EquipCheckingTaskDao equipCheckingTaskDao;

	@Autowired
	private EquipCheckingTaskDao taskDao;
	@Autowired
	private JdbcDao jdbcDao;

	@Override
	public void deleteCheckingRules(List<Integer> ids) {
		if (ids.size() > 0 && ids != null) {
			equipCheckingRuleDao.deleteCheckingRules(ids);
		}
	}

	@Override
	public void saveCheckingOrderItems(EquipCheckItemParsmBean bean) {
		// 查询需要保存的点检规则
		List<EquipCheckingItems> checkingItems = equipCheckingOrderDao
				.queryCheckOrderItemsInfoByRuleIds(bean.getEquipCheckingItems());
		if (checkingItems != null && checkingItems.size() > 0) {

			Map<Integer, EquipCheckingItems> map = new HashMap<>();

			for (EquipCheckingItems equipCheckingItems : bean.getEquipCheckingItems()) {
				map.put(equipCheckingItems.getRuleId(), equipCheckingItems);
			}

			for (EquipCheckingItems item : checkingItems) {
				item.setTool(map.get(item.getRuleId()).getTool());
				item.setToolCount(map.get(item.getRuleId()).getToolCount());
				item.setToolUnit(map.get(item.getRuleId()).getToolUnit());
				item.setActualValue(map.get(item.getRuleId()).getActualValue());
				item.setOrderId(bean.getOrderId());
				item.setCreateTime(new Date());
				item.setCreateUser(SpringWebUtils.getRequestUser());
				item.setLastUpdateTime(new Date());
				item.setLastUpdateUser(SpringWebUtils.getRequestUser());
				item.setCompanyCode(SpringWebUtils.getRequestCompany());
			}
			// 保存点检项
			equipCheckingOrderDao.saveCheckingOrderItems(checkingItems);
		}
	}

	@Override
	public void updateCheckingOrderItems(EquipCheckItemParsmBean bean) {
		for (EquipCheckingItems e : bean.getEquipCheckingItems()) {
			e.setMaintainer(SpringWebUtils.getRequestUser());
		}
		equipCheckingOrderDao.updateCheckingOrderItems(bean.getEquipCheckingItems());
	}

	@Override
	public void editCheckingOrderItems(EquipCheckItemParsmBean bean) {
		// 删除已有点检项
		equipCheckingOrderDao.deleteCheckingOrderItems(bean.getOrderId());
		// 新增点检项
		this.saveCheckingOrderItems(bean);
	}

	@Override
	public void deleteCheckingOrders(Integer checkId) {
		if (checkId != null && checkId > 0) {
			// 删除点检工单
			equipCheckingOrderDao.deleteCheckingOrders(checkId);
		}
	}

	@Override
	public void addCheckingProgramOrder(EquipCheckingProgramOrder bean) {

		if (bean != null) {
			bean.setCreateTime(new Date());
			bean.setLastUpdateTime(new Date());
			bean.setCreateUser(SpringWebUtils.getRequestUser());
			bean.setLastUpdateUser(SpringWebUtils.getRequestUser());
			bean.setCompanyCode(SpringWebUtils.getRequestCompany());
			List<EquipCheckingProgramStandard> ruleList = bean.getRuleList();
			// 新增点检项目,返回新增的id
			equipCheckingOrderDao.addCheckingProgramOrder(bean);
			if (ruleList != null && ruleList.size() != 0) {
				for (EquipCheckingProgramStandard rule : ruleList) {
					rule.setOrderId(bean.getId());
					rule.setCreateTime(new Date());
					rule.setLastUpdateTime(new Date());
					rule.setCreateUser(SpringWebUtils.getRequestUser());
					rule.setLastUpdateUser(SpringWebUtils.getRequestUser());
					rule.setCompanyCode(SpringWebUtils.getRequestCompany());
				}
				// 新增点检项目标准
				equipCheckingOrderDao.addCheckingProgramStandard(ruleList);
			}
		}

	}

	@Override
	public void updateCheckingProgramOrder(EquipCheckingProgramOrder bean) {
		if (bean != null) {
			bean.setLastUpdateTime(new Date());
			bean.setLastUpdateUser(SpringWebUtils.getRequestUser());
			equipCheckingOrderDao.updateCheckingProgramOrder(bean);
			List<EquipCheckingProgramStandard> ruleList = bean.getRuleList();
			equipCheckingOrderDao.deleteCheckingProgramStandard(bean.getId());
			if (ruleList != null && ruleList.size() != 0) {
				for (EquipCheckingProgramStandard rule : ruleList) {
					rule.setOrderId(bean.getId());
					rule.setCreateTime(new Date());
					rule.setLastUpdateTime(new Date());
					rule.setCreateUser(SpringWebUtils.getRequestUser());
					rule.setLastUpdateUser(SpringWebUtils.getRequestUser());
					rule.setCompanyCode(SpringWebUtils.getRequestCompany());
				}
				equipCheckingOrderDao.addCheckingProgramStandard(ruleList);
			}

		}
	}

	@Override
	public void deleteCheckingProgramOrder(Integer id) {
		// 通过id删除点检项目
		equipCheckingOrderDao.deleteCheckingProgramOrder(id);

		// 通过点检id删除点检标准
		equipCheckingOrderDao.deleteCheckingProgramStandard(id);

	}

	@Override
	public EquipCheckingTaskBean queryTaskList(EquipCheckingTaskBean taskBean) {
		taskBean.setUsername(SpringWebUtils.getRequestUser());
		taskBean.setResultList(equipCheckingOrderDao.queryTaskList(taskBean));
		taskBean.setTotalRows(equipCheckingOrderDao.queryTaskCount(taskBean));

		return taskBean;
	}

	@Override
	public List<EquipCheckingTaskDetailsBean> getCheckingTaskDetails(Integer taskId) {

		return equipCheckingOrderDao.getCheckingTaskDetails(taskId);
	}

	@Override
	public void updateCheckingTaskStatus(Integer taskId, String status) {
		equipCheckingOrderDao.updateCheckingTaskStatus(taskId, status);
	}

	@Override
	public void saveCheckingTask(EquipCheckingTaskDetailsBean taskDetails) {

		List<EquipCheckingTaskProgramItem> programItems = taskDetails.getItemList();
		for (EquipCheckingTaskProgramItem item : programItems) {
			item.setLastUpdateTime(new Date());
			item.setLastUpdateUser(SpringWebUtils.getRequestUser());
		}
		taskDao.updateEquipCheckingTaskProgramItem(programItems);
		
		String generateFormId = taskDetails.getGenerateFormId();
		taskDao.saveGenerateFormId(generateFormId, taskDetails.getTaskProgramId());
	}
	
	@Override
	public List<EquipCheckingTaskProgramItem> getCheckingProgramDetails(Integer programId) {
		
		return taskDao.getCheckingProgramDetails(programId);
	}

	@Override
	public void addCheckingTask(EquipCheckPlanBean bean) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date startTime = null;
		Date endTime = null;
		try {
			startTime = sdf.parse(bean.getPlanStartTime());
			endTime = sdf.parse(bean.getPlanEndTime());
		} catch (ParseException e) {
			throw new ServiceException("时间格式异常");
		}
		//处理计划下的任务
		List<EquipCheckingTask> list = ExplanPlan.explanPlan(startTime, endTime, bean.getUnit(), bean.getFrequency());
		if (list != null && list.size() > 0) {
			for (EquipCheckingTask e : list) {
				e.setPlanId(bean.getId());
				e.setTaskNo(jdbcDao.executeProcGetCode("DJ", SpringWebUtils.getRequestCompany()));
				e.setStatus("todo");
				e.setOrgId(bean.getOrgId());
				e.setOrgName(bean.getOrgName());
				e.setAuditor(bean.getAuditor());
				e.setCreateTime(new Date());
				e.setLastUpdateTime(new Date());
				e.setCreateUser(SpringWebUtils.getRequestUser());
				e.setLastUpdateUser(SpringWebUtils.getRequestUser());
				e.setCompanyCode(SpringWebUtils.getRequestCompany());
			}
			//保存计划下的任务
			equipCheckingTaskDao.addEquipCheckTask(list);
			//处理任务下的点检项目
			List<EquipCheckingTaskEquipBean> equipList = new ArrayList<>();
			if (bean.getEquipProgram() != null && bean.getEquipProgram().size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					for (int j = 0; j < bean.getEquipProgram().size(); j++) {
						EquipCheckingTaskEquipBean p = new EquipCheckingTaskEquipBean();
						p.setProgramList(bean.getEquipProgram().get(j).getProgramList());
						p.setTaskId(list.get(i).getId());
						p.setPlanId(list.get(i).getPlanId());
						p.setEquipId(bean.getEquipProgram().get(j).getEquipId());
						p.setClassId(bean.getEquipProgram().get(j).getClassId());
						p.setEquipCode(bean.getEquipProgram().get(j).getEquipCode());
						p.setEquipName(bean.getEquipProgram().get(j).getEquipName());
						p.setClassName(bean.getEquipProgram().get(j).getClassName());
						p.setEquipModel(bean.getEquipProgram().get(j).getEquipModel());
						p.setCreateTime(new Date());
						p.setLastUpdateTime(new Date());
						p.setCreateUser(SpringWebUtils.getRequestUser());
						p.setLastUpdateUser(SpringWebUtils.getRequestUser());
						p.setCompanyCode(SpringWebUtils.getRequestCompany());
						equipList.add(p);
					}
				}
				//保存任务关联的设备
				if (equipList != null && equipList.size() > 0) {
					equipCheckingTaskDao.addEquipCheckingTaskEquip(equipList);
				}
			}else{
				throw new ServiceException("请选择设备");
			}

			//设备中选择的项目
			List<EquipCheckingTaskProgram> programList = new ArrayList<>();
			for (int j = 0; j < equipList.size(); j++) {
				if (equipList.get(j).getProgramList() != null && equipList.get(j).getProgramList().size() > 0) {
					for (int k = 0; k < equipList.get(j).getProgramList().size(); k++) {
						EquipCheckingTaskProgram p = new EquipCheckingTaskProgram();
						p.setTaskId(equipList.get(j).getTaskId());
						p.setEquipId(equipList.get(j).getEquipId());
						p.setTaskEquipId(equipList.get(j).getId());
						p.setClassId(equipList.get(j).getClassId());
						p.setEquipCode(equipList.get(j).getEquipCode());
						p.setEquipName(equipList.get(j).getEquipName());
						p.setClassName(equipList.get(j).getClassName());
						p.setProgramId(equipList.get(j).getProgramList().get(k).getProgramId());
						p.setProgramName(equipList.get(j).getProgramList().get(k).getProgramName());
						p.setCheckPurpose(equipList.get(j).getProgramList().get(k).getCheckPurpose());
						p.setCreateTime(new Date());
						p.setLastUpdateTime(new Date());
						p.setCreateUser(SpringWebUtils.getRequestUser());
						p.setLastUpdateUser(SpringWebUtils.getRequestUser());
						p.setCompanyCode(SpringWebUtils.getRequestCompany());
						programList.add(p);
					}
				}else{
					throw new ServiceException("设备必须选择项目");
				}
			}
			//保存点检项目
			if (programList != null && programList.size() > 0) {
				equipCheckingTaskDao.addEquipCheckingTaskProgram(programList);
				//保存项目的点检标准
				List<EquipCheckingProgramStandard> standardList = equipCheckingTaskDao.queryEquipCheckingProgramStandard(programList);

				Map<Integer, EquipCheckingProgramStandard> map = new HashMap<>();
				List<EquipCheckingTaskProgramItem> itemList = new ArrayList<>();
				if (standardList != null && standardList.size() > 0) {
					for (int i = 0; i < programList.size(); i++) {
						for (int j = 0; j < standardList.size(); j++) {
							if (programList.get(i).getProgramId().equals(standardList.get(j).getOrderId())) {
								EquipCheckingTaskProgramItem item = new EquipCheckingTaskProgramItem();
								item.setTaskId(programList.get(i).getTaskId());
								item.setTaskProgramId(programList.get(i).getId());
								item.setStandardId(standardList.get(j).getId());
								item.setProgramId(standardList.get(j).getOrderId());
								item.setCheckingItem(standardList.get(j).getCheckingItem());
								item.setCheckingType(standardList.get(j).getCheckingType());
								item.setShutdown(standardList.get(j).getShutdown());
								item.setStandardValue(standardList.get(j).getStandardValue());
								item.setRemark(standardList.get(j).getRemark());
								item.setLastUpdateTime(new Date());
								item.setLastUpdateUser(SpringWebUtils.getRequestUser());
								item.setCompanyCode(SpringWebUtils.getRequestCompany());
								item.setCreateTime(new Date());
								item.setCreateUser(SpringWebUtils.getRequestUser());
								itemList.add(item);
							}
						}
					}
				}
				//插入数据
				if (itemList != null && itemList.size() > 0) {
					equipCheckingTaskDao.addEquipCheckingTaskProgramItem(itemList);
				}
			}

		}else{
			throw new ServiceException("所选时间无法生成任务");
		}
	}
}
